Time Series Data Visualization

Importing Libraries
library(tidyverse)
library(ggplot2)
library(forecast)
library(astsa) 
library(xts)
library(tseries)
library(fpp2)
library(fma)
library(lubridate)
library(tidyverse)
library(TSstudio)
library(quantmod)
library(tidyquant)
library(plotly)
library(ggplot2)
library(imputeTS)
library(gridExtra)
library(reticulate)
library(readxl)
use_python("C:/Users/nibh/anaconda3/envs/python36/python.exe", require = T)
knitr::knit_engines$set(python = reticulate::eng_python)

Time Series Data Visualization

In time series data visualization, the importance lies in presenting temporal patterns and trends in a clear and comprehensible manner. Effective visualization allows analysts and decision-makers to extract meaningful insights from the data, aiding in better understanding the dynamics of a system over time. The choice of visualization techniques is crucial, as it directly influences the interpretation of patterns within the time series.

The ability to discern seasonality, identify anomalies, and recognize patterns is vital for making informed predictions and strategic decisions. Furthermore, interactive features in visualizations enable users to delve deeper into the data, offering a dynamic and exploratory experience.

Ultimately, the clarity and accuracy of time series data visualization contribute significantly to enhancing decision-making processes across various domains, such as finance, healthcare, environmental monitoring and many other areas.

The following graph shows overall trends in Disney, Microsoft, and Netflix.

Code
options("getSymbols.warning4.0"=FALSE)
options("getSymbols.yahoo.warning"=FALSE)

tickers = c("NFLX","MSFT","DIS" )
for (i in tickers){
  getSymbols(i,
             from = "2013-01-01",
             to = "2024-12-31")}

x <- list(
  title = "date"
)
y <- list(
  title = "value"
)

stock <- data.frame(NFLX$NFLX.Adjusted,
                    MSFT$MSFT.Adjusted,
                    DIS$DIS.Adjusted)


stock <- data.frame(stock,rownames(stock))
colnames(stock) <- append(tickers,'Dates')

stock$date<-as.Date(stock$Dates,"%Y-%m-%d")
head(stock)
               NFLX     MSFT      DIS      Dates       date
2013-01-02 13.14429 22.36213 45.88776 2013-01-02 2013-01-02
2013-01-03 13.79857 22.06256 45.98654 2013-01-03 2013-01-03
2013-01-04 13.71143 21.64965 46.86657 2013-01-04 2013-01-04
2013-01-07 14.17143 21.60916 45.77102 2013-01-07 2013-01-07
2013-01-08 13.88000 21.49582 45.58245 2013-01-08 2013-01-08
2013-01-09 13.70143 21.61726 45.60040 2013-01-09 2013-01-09
Code
################################################

ggplot(stock, aes(x=date)) +
  geom_line(aes(y=NFLX, colour="NFLX"))+
  geom_line(aes(y=MSFT, colour="MSFT"))+
  geom_line(aes(y=DIS, colour="DIS"))+
   labs(
    title = "Stock Prices for the Tech Companies",
    subtitle = "From 2013-2024",
    x = "Date",
    y = "Adjusted Closing Prices")+
    theme(panel.background = element_rect(fill = "white", colour = "grey50"))+
    guides(colour=guide_legend(title="Tech Companies")) 

The plot above shows the stock price trends of Disney (DIS), Microsoft (MSFT), and Netflix (NFLX) from 2013 to 2024. Overall, these stock prices exhibit an upward trend. In particular, Netflix saw a rapid rise in stock price from 2016 to 2020, with its price reaching twice that of Microsoft in 2021. However, in 2022, the stock prices of all three companies declined, especially Netflix, which saw a sharp drop followed by a slight recovery. This could be related to the post-pandemic decrease in consumer demand, particularly for Netflix’s video content. Compared to Netflix and Microsoft, Disney’s stock price remained relatively lower and more stable.

Code
#plotly
# candlestick plot

getSymbols("ETH-USD", from = "2024-04-01", to = "2024-05-01")
[1] "ETH-USD"
Code
df <- data.frame(Date = index(`ETH-USD`), coredata(`ETH-USD`))
df <- tail(df, 30)
colnames(df) <- c("Date", "Open", "High", "Low", "Close", "Volume")

figc <- df %>% plot_ly(x = ~Date, type = "candlestick",
                       open = ~Open, close = ~Close,
                       high = ~High, low = ~Low)
figc <- figc %>% layout(title = "Ethereum (ETH) Candlestick Plot")

figc

Ethereum (ETH) is a decentralized cryptocurrency widely used in blockchain technology. The candlestick plot above shows the price trend of ETH-USD during April 2024. Each candlestick represents four price points: Open, Close, High, and Low. The color of the candlestick typically indicates whether the price has gone up (green) or down (red) within a day. In April, ETH’s price fluctuated significantly, reaching a high of $3727 and a low of $2815 on May 1. Although the price experienced fluctuations, the overall trend showed a decline.

Hover over the interactive plot to see the difference.

Code
library(dotenv)
library(fredr)

fredr_set_key(trimws(Sys.getenv("FRED_API_KEY")))

gdp_data <- fredr(series_id = "GDP", 
                  observation_start = as.Date("2013-01-01"), 
                  observation_end = as.Date("2024-12-31"))

fig <- gdp_data %>%
  plot_ly(x = ~date, y = ~value, type = 'scatter', mode = 'lines', 
          line = list(color = 'blue')) %>%
  layout(title = 'US GDP',
         subtitle = 'From 2013-2024',
         xaxis = list(title = 'Date'),
         yaxis = list(title = 'GDP in Billions of USD'))

fig

U.S. GDP (Gross Domestic Product) reflects the overall size and growth of the U.S. economy. The plot above shows the quarterly GDP of the U.S. from 2013 to 2024. It is evident that the U.S. GDP has generally shown steady growth, indicating the continuous expansion of the U.S. economy during this period, with inflation contributing to this growth. However, at the beginning of 2020, the GDP saw a significant decline due to the global pandemic, but it gradually recovered during the subsequent economic rebound.